angular.module("application")

// .directive('lessoncontent', function($compile) {
// 	return {
// 		restrict: 'A',
// 		replace: true,
// 		scope: "",
// 		controller: function($scope) {
// 			$scope.alala = "None way";
// 		},
// 		link: function(scope, element, attrs) {
// 			scope.$watch(attrs.lessoncontent, function() {
// 				element.html(scope.content);
// 				$compile(element.contents())(scope);
// 			});
// 			element.bind('click', function(content) {
// 				alert(attrs.lessoncontent);
// 				element.html(content);
// 				$compile(element.contents())(scope);
// 			})
// 		}
// 	}
// }) // Làm hoài không được =.="

.controller('lessonCtrl', function($scope, $http, $location,$routeParams,$sce,$timeout,
	lessonProcess, mapProcess, commentProcess, questionProcess, BASE_URL) 
{	

	$scope.lessonId = $routeParams.lessonId;
	$scope.content = "";

	$scope.timelineView = false;
	$scope.timelineData = [];
	$scope.timelineIndex = 0;
	$scope.mapIds = [];
	$scope.comments = [];

	$scope.newTopic = '';
	$scope.replyCmt = {
		lsId: $scope.lessonId
	};

	$scope.isBookmark = false;

	$scope.$on('$viewContentLoaded', function() { // Load content effexts after content is loaded
		construct();
	});

	var lsData = lessonProcess.getData($scope.lessonId)
	.then(function(data) {
		$scope.title = data.title;	
		$scope.content = $sce.trustAsHtml(data.content);	

		$scope.timelineData = data.timeline;
		if($scope.timelineData) {
			$scope.getTimelineContent(0);
		}

		$timeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			});
			$('.lesson-maps').each(function() {
				$scope.mapIds.push($(this).attr('id'));
			});
			
		},200)
		.then(function() {
			// console.log($scope.maps);
			var maps = $scope.mapIds;
			for(m in maps) {
				mapProcess.initialMap(maps[m]);
			};
		})

		return {
			getCat: lessonProcess.getCat(data.cat),
			getQuestion: questionProcess.loadQuestions(data.questions),
		}
	}).then(function(actions) {

		//Render Table view;
		$('.lessoncontent table').addClass('.table-bordered');	

		actions.getCat.then(function(data) {
			if(data.type == "vn") {
				$scope.catType = {
					"id": "vn",
					"str": "LS Việt Nam"
				}
			}
			else if(data.type == "wo") {
				$scope.catType = {
					"id": "wo",
					"str": "LS Thế giới"
				}
			}
			$scope.cat = data;
		});
		
		actions.getQuestion.then(function(data) {
			$scope.questionList = data;
			lessonProcess.sliderList('#question-list');
			setTimeout(function() {
				$('#question-list').sly('reload');
			},200);
		});
	});

	commentProcess.loadCommentData($scope.lessonId).then(function(promise) {
		$scope.comments = promise;
	});

	$scope.setLessonView = function() {
		
		$scope.even = 1;		
		$scope.timelineView = !($scope.timelineView);

		lessonProcess.stopVideos();

		setTimeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = true;
			});
		},20);
		setTimeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			});
		},1000);
		setTimeout(function() {
			mapProcess.intialTimelineMap();
			timelineMap = mapProcess.resizeMap();
			if($scope.timelineIndex != null)
				mapProcess.setTimelineMap($scope.timelineData[$scope.timelineIndex].maps);
			renderTimelineColumn();
		}, 1020);
		lessonProcess.timelineRender($scope.timelineData[$scope.timelineIndex].paragraph);
		renderTimelineColumn();

	}
	$scope.getTimelineContent = function(index) {
		$scope.timelineIndex = index;
		lessonProcess.stopVideos();
		lessonProcess.timelineRender($scope.timelineData[index].paragraph);
		mapProcess.setTimelineMap($scope.timelineData[index].maps);
		renderTimelineColumn();
	}
	$scope.popupQuestion = function(question) {
		$scope.pQ = {};
		$scope.pQ.question = question.question;
		$scope.pQ.answer = $sce.trustAsHtml(question.answer);
		$('#popup-question').modal('show');
	}
	$scope.addTopic = function() {
		str = $scope.newTopic;
		if(str.length < 20) {
			getAlert('Nội dung thảo luận quá ngắn');
			return;
		}
		commentProcess.addTopic(str, $scope.lessonId, 'null').then(function(msg) {
			if(msg == 'success') {
				getAlert('Bình luận của bạn chờ duyệt');
				$scope.newTopic = '';
				return commentProcess.loadCommentData($scope.lessonId).then(function(promise) {
					$scope.comments = promise;
				});
			}
		})	
	}
	$scope.replyComment = function() {
		str = $scope.replyCmt.content;
		lsId = $scope.replyCmt.lsId;
		par = $scope.replyCmt.parent; 
		if(str.length < 20) {
			getAlert('Nội dung thảo luận quá ngắn');
			return;
		}		
		
		commentProcess.addTopic(str, lsId, par).then(function(msg) {
			if(msg == 'success') {
				getAlert('Bình luận của bạn chờ duyệt');
				$scope.replyCmt.content = '';
				$('#popup-comment').modal('hide');

				return commentProcess.loadCommentData($scope.lessonId).then(function(promise) {
					$scope.comments = promise;
				});
			}
		})	
	}

	$scope.toggleBookmark = function() {
		if(!$scope.$parent.isLogin) {
			getAlert('Vui lòng đăng nhập để đánh dấu bài học quan tâm');
			return;
		}

		if(!$scope.isBookmark) {
			$http.post(BASE_URL + 'user/addBookmark/' + $scope.lessonId)
			.then(function(prm) {
				if(prm.data == 'success') {
					$scope.isBookmark = true;
				}
			});
			return;
		}
		
		$http.post(BASE_URL + 'user/removeBookmark/' + $scope.lessonId)
		.then(function(prm) {
			if(prm.data == 'success') {
				$scope.isBookmark = false;
			}
		});
	}

	getSesson = function() {
		return $scope.$parent.isLogin;
	}
	$scope.$watch(getSesson, function(val) {
		if(!val) $scope.isBookmark = false;
		else {
			$http.post(BASE_URL + 'user/check_session')
			.then(function(prm) {
				if(prm.data == '901nosession') {
					return false;
				}
				else return $http.post(BASE_URL + 'user/getBookmark')
			}).then(function(prm) {
				for(i in prm.data) {
					if($scope.lessonId == prm.data[i]){
						$scope.isBookmark = true;
					}
				}
			})
		}
	});

	// Render Html
	function renderTimelineColumn() {
		timelineViewHeight = $('.timeline-view').height();
		titleHeight = $('.timeline-view .lesson-title').height();
		$('.timeline-col').css('min-height', timelineViewHeight - 58);
	}

	$(window).resize(function() {
		renderTimelineColumn();
	});
});